#include "AbstractPanel.h"
#include "GraphicsText.h"
#include "AbstractMappingWindow.h"
#include "AbstractManager.h"
#include "PanelItem.h"
#include <cassert>
#include <QFont>
#include <QTextDocument>
#include <QPainter>

AbstractPanel::AbstractPanel( std::string name, AbstractManager* manager )
:AbstractShapeItem(name, manager)
{
	this->_width = 154;
}

AbstractPanel::~AbstractPanel()
{

}

void AbstractPanel::initTitle(QString name, bool isEditable)
{
	QFont sansFont("Helvetica", 9);
	sansFont.setBold(true);

	_title = new GraphicsText( name, this );
	_title->setFont(sansFont);
	_title->setDefaultTextColor(Qt::white);

	_title->setAlignment(Qt::AlignCenter);
	_title->setPos( this->_width / 2 - _title->boundingRect().width() / 2 , TITLE_BLOCK_HEIGHT / 2 - _title->boundingRect().height() / 2 );
	_title->setEditable(isEditable);
}

void AbstractPanel::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget)
{
	// 畫 Panel 的背景
	QPixmap backgroundPixmap(":/resource/gates_panel_bg.png");
	QBrush backgroundBrush(backgroundPixmap);
	painter->setBrush(backgroundBrush);
	painter->setPen(Qt::NoPen);
	painter->drawRect(0, 0, backgroundPixmap.width(), AbstractMappingWindow::SCENE_HEIGHT);

	// 畫 Panel title 的 Block
	painter->setBrush(QBrush(QColor("#222222")));
	painter->drawRect(0, 0, this->_width, TITLE_BLOCK_HEIGHT);
}

QRectF AbstractPanel::boundingRect() const
{
	QPixmap backgroundPixmap(":/resource/gates_panel_bg.png");
	return QRectF(0, 0, backgroundPixmap.width(), AbstractMappingWindow::SCENE_HEIGHT);
}

void AbstractPanel::setItemColor( int index, AbstractShapeItem *item )
{
	if ( index % 2 == 1){
		item->setBackgroundColor(QColor("#F2F5F9"));
	} 
}

QString AbstractPanel::getTitleString()
{
	return _title->toPlainText();
}

void AbstractPanel::setTitleString( QString title)
{
	_title->setPlainText(title);
}

void AbstractPanel::deleteItem( PanelItem* item)
{
	_manager->deletePanelItem(item);

	std::vector<PanelItem*>::iterator itr;
	for ( itr = _itemList.begin(); itr != _itemList.end(); ++itr )
	{
		if ( *itr == item ){
			_itemList.erase(itr);
			break;
		}
	}
	delete item;
}

void AbstractPanel::insertItem( PanelItem* item )
{
	assert(item);

	int index = _itemList.size();
	insertItem(item, index);

}

void AbstractPanel::insertItem( PanelItem* item, int index )
{
	assert(item);

	_itemList.insert( std::next( _itemList.begin(), index ), item );

	// 設定 GateItem 顏色與位置，並把 GateItem 加到畫布上
	setItemColor( index , item);
	updateItemsPosition();
}

std::vector<PanelItem*>& AbstractPanel::getItems()
{
	return _itemList;
}

int AbstractPanel::indexOfItem( PanelItem* item )
{
	int index = 0;

	for ( int i = 0; i < _itemList.size(); ++i )
	{
		if ( _itemList.at(i) == item){
			index = i;
			break;
		}
	}

	return index;
}

void AbstractPanel::updateItemsPosition()
{
	int visiableItemCount = 0;
	for ( int i = 0; i < _itemList.size(); ++i )
	{
		PanelItem* item = _itemList.at(i);
		if ( item->isVisible() == true){
			_itemList.at(i)->setPos(0, visiableItemCount* PanelItem::HEIGHT + TITLE_BLOCK_HEIGHT);
			visiableItemCount++;
		}
	}

	this->update();
}
